<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","nobkt");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_autoex.php";
$FileTemplate  = "ais_transaction_autoex_list.html";
$TableName     = "ais_memorial";
$SubHeaderTitle= "Transaction Exchange Gain/Loss - Automatic Exchange";
$PageSize      = 10;
$HTMLGridList  = "MemorialGrid";


//Initiate Grid Fields;
//none

//************ FORM ACTION **************
    if (GetParam("Action","")=="CreateExchange") {

        //print_r($_POST);
        //$noBkt = $_POST['txtCheck'];

        /*
        $nomor = $_POST['txtCheck'];
        $kozhie = count($nomor);
        echo "$kozhie <br>";

        if(count($nomor) > 0){
          foreach($nomor as $rowi){
            echo $rowi. '<br />';
          }
        }
        */

        $noBkt = $_POST['txtCheck'];

        if(count($noBkt) > 0){
            foreach($noBkt as $row){
                echo $row . '<br />';
            /*
            $SQL = "SELECT nobkt FROM ais_memorial WHERE nobkt = '$row'";
            $getNoBkt = $DBConnection->dbc->get_var($SQL);
            echo "nobkt adalah : $getNoBkt <br>";

            $SQL = "SELECT noref FROM ais_memorial WHERE nobkt = '$row'";
            $getNoRef = $DBConnection->dbc->get_var($SQL);
            echo "noref adalah : $getNoRef <br>";

            $SQL = "SELECT debnam FROM ais_memorial WHERE nobkt = '$row'";
            $getDebnam = $DBConnection->dbc->get_var($SQL);
            echo "debnam adalah : $getDebnam <br>";

            $SQL = "
            SELECT if (sum(ais_memorial.jumlah)>sum(ref.jumlah),sum(ref.jumlah),sum(ais_memorial.jumlah)) as jumlah FROM ais_memorial
            LEFT JOIN ais_memorial ref ON (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada AND ais_memorial.uraian=ref.uraian) OR (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada)
            LEFT JOIN ais_memorial refmm ON (refmm.noref=ais_memorial.noref AND refmm.urut=999 AND refmm.nobkt like 'MM%')
            LEFT JOIN ais_memorial mx ON mx.noref=ais_memorial.nobkt AND mx.nobkt like 'MX%'
            WHERE ".QueryLedger("ais_memorial")." AND
            ais_memorial.nobkt = '$row' AND
            ais_memorial.rate>1 AND
            ais_memorial.noref<>'' AND ais_memorial.rate<>if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate)
            AND ifnull(mx.nobkt,'')='' AND NOT (SUBSTRING(ais_memorial.nobkt,1,1)='T' AND SUBSTRING(ais_memorial.noref,1,1)='T') AND NOT (ais_memorial.skada LIKE '0002%' OR ais_memorial.skada LIKE '0001%' OR ais_memorial.skada LIKE '6%' OR ais_memorial.skada LIKE '7%' OR ais_memorial.skada LIKE '8%' OR ais_memorial.skada LIKE '9%')
            GROUP BY ais_memorial.noref, ais_memorial.nobkt ORDER BY ais_memorial.tglbkt DESC
            ";

            $getJumlah = $DBConnection->dbc->get_var($SQL);
            echo "jumlah adalah : $getJumlah <br>";
`           */

            $SQL = "
            SELECT ais_memorial.nobkt, ais_memorial.noref, if (sum(ais_memorial.jumlah)>sum(ref.jumlah),sum(ref.jumlah),sum(ais_memorial.jumlah)) as jumlah,
            ais_memorial.rate as forex, ais_memorial.tglbkt as date,
            if (sum(ais_memorial.jumlah)>sum(ref.jumlah),sum(ref.jumlah),sum(ais_memorial.jumlah))*(if (SUBSTRING(ais_memorial.nobkt,1,1)='T',ais_memorial.rate-if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate),if (SUBSTRING(ais_memorial.nobkt,1,1)='N',ais_memorial.rate-if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate),if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate)-ais_memorial.rate))) as amount,
            ais_memorial.debnam,
            if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate) as rate
            FROM ais_memorial
            LEFT JOIN ais_memorial ref ON (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada AND ais_memorial.uraian=ref.uraian) OR (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada)
            LEFT JOIN ais_memorial refmm ON (refmm.noref=ais_memorial.noref AND refmm.urut=999 AND refmm.nobkt like 'MM%')
            LEFT JOIN ais_memorial mx ON mx.noref=ais_memorial.nobkt AND mx.nobkt like 'MX%'
            WHERE ".QueryLedger("ais_memorial")." AND
            ais_memorial.nobkt = '$row' AND
            ais_memorial.rate>1 AND
            ais_memorial.noref<>'' AND ais_memorial.rate<>if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate)
            AND ifnull(mx.nobkt,'')='' AND NOT (SUBSTRING(ais_memorial.nobkt,1,1)='T' AND SUBSTRING(ais_memorial.noref,1,1)='T') AND NOT (ais_memorial.skada LIKE '0002%' OR ais_memorial.skada LIKE '0001%' OR ais_memorial.skada LIKE '6%' OR ais_memorial.skada LIKE '7%' OR ais_memorial.skada LIKE '8%' OR ais_memorial.skada LIKE '9%')
            GROUP BY ais_memorial.noref, ais_memorial.nobkt ORDER BY ais_memorial.tglbkt DESC
            ";

            $testresult = mysql_query($SQL);
            $getrow = mysql_fetch_row($testresult);

            echo "test SQL RAMA : <br>";
            echo "nobkt : ".$getrow[0]. "<br>";
            echo "noref: ".$getrow[1]. "<br>";
            echo "debnam : ".$getrow[6]. "<br>";
            echo "jumlah : ".$getrow[2]. "<br>";
            echo "rate : ".$getrow[7]. "<br>";
            echo "forex : ".$getrow[3]. "<br>";
            echo "jumlah : ".$getrow[5]. "<br>";


            //header for MP
            list($day,$month,$year) = explode("/",date("d/m/Y"));
            $tglbkt  = "$day"."/"."$month"."/"."$year";
            $ObjNumber  = new autonumber("$tglbkt");
            $TransNo = $ObjNumber->GetNumber("MP");

            //insert to table ais_gainloss_status
            $DBConnection->add_data("ais_gainloss_status","nomp",$TransNo);
            $DBConnection->add_data("ais_gainloss_status","nobkt",$getrow[0]);
            $DBConnection->add_data("ais_gainloss_status","noref",$getrow[1]);
            $DBConnection->add_data("ais_gainloss_status","debnam",$getrow[6]);
            $DBConnection->add_data("ais_gainloss_status","stjumlah",$getrow[2]);
            $DBConnection->add_data("ais_gainloss_status","rate",$getrow[7]);
            $DBConnection->add_data("ais_gainloss_status","forex",$getrow[3]);
            $DBConnection->add_data("ais_gainloss_status","jumlah",$getrow[5]);

            $DBConnection->InsertGrid();
            }
        }
        //ExchangeRecord ("txtCheck",$PageSize);
    }

    //exit;

//Initiate Database Table For Search;
$FCode = GetParam("transnoSearch","");


//******* DATABASE FORM QUERY ***********
$SQL = "

SELECT nobkt,noref,tglbkt,rate,sum(jumlah),debnam
FROM `$TableName`

";
$SQLCount = "SELECT count(*) FROM `$TableName` ";
$WHERE  = "ktrasl='".GetOfficeID()."' AND ".QueryLedger()." AND
          ((nobkt like 'TB%' AND noref like 'ND%') OR
           (nobkt like 'TK%' AND noref like 'ND%') OR
           (nobkt like 'BB%' AND noref like 'MM%') OR
           (nobkt like 'BK%' AND noref like 'MM%') OR
           (nobkt like 'NK%' AND noref like 'ND%')) AND rate>1 ";

//Retrieve Parameter from search
if ($FCode) {
    $CustomWhere = " nobkt like '".$FCode."%'";
    if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
    else $WHERE = $CustomWhere;
}

if (strlen($WHERE)) {
    $SQL  = $SQL."WHERE ".$WHERE;
    $SQLCount .= "WHERE ".$WHERE;
}


//print $SQL;

$SQL = "
  SELECT ais_memorial.nobkt, ais_memorial.noref, if (sum(ais_memorial.jumlah)>sum(ref.jumlah),sum(ref.jumlah),sum(ais_memorial.jumlah)) as jumlah,
             ais_memorial.rate as forex, ais_memorial.tglbkt as date,
             if (sum(ais_memorial.jumlah)>sum(ref.jumlah),sum(ref.jumlah),sum(ais_memorial.jumlah))*(if (SUBSTRING(ais_memorial.nobkt,1,1)='T',ais_memorial.rate-if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate),if (SUBSTRING(ais_memorial.nobkt,1,1)='N',ais_memorial.rate-if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate),if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate)-ais_memorial.rate))) as amount,
             ais_memorial.debnam,
             if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate) as rate, nomp
  FROM ais_memorial
  LEFT JOIN ais_memorial ref ON (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada AND ais_memorial.uraian=ref.uraian) OR (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada)
  LEFT JOIN ais_memorial refmm ON (refmm.noref=ais_memorial.noref AND refmm.urut=999 AND refmm.nobkt like 'MM%')
  LEFT JOIN ais_memorial mx ON mx.noref=ais_memorial.nobkt AND mx.nobkt like 'MX%'
  LEFT JOIN ais_gainloss_status ON ais_gainloss_status.nobkt = ais_memorial.nobkt
  WHERE ".QueryLedger("ais_memorial")." AND
  ais_memorial.rate>1 AND
  ais_memorial.noref<>'' AND ais_memorial.rate<>if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate)
  AND ifnull(mx.nobkt,'')='' AND NOT (SUBSTRING(ais_memorial.nobkt,1,1)='T' AND SUBSTRING(ais_memorial.noref,1,1)='T') AND NOT (ais_memorial.skada LIKE '0002%' OR ais_memorial.skada LIKE '0001%' OR ais_memorial.skada LIKE '6%' OR ais_memorial.skada LIKE '7%' OR ais_memorial.skada LIKE '8%' OR ais_memorial.skada LIKE '9%')
  AND nomp IS NULL
";

$SQLCount = "
  SELECT count(*)
  FROM ais_memorial
  LEFT JOIN ais_memorial ref ON (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada AND ais_memorial.uraian=ref.uraian) OR (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada)
  LEFT JOIN ais_memorial refmm ON (refmm.noref=ais_memorial.noref AND refmm.urut=999 AND refmm.nobkt like 'MM%')
  LEFT JOIN ais_memorial mx ON mx.noref=ais_memorial.nobkt AND mx.nobkt like 'MX%'
  WHERE ".QueryLedger("ais_memorial")." AND
  ais_memorial.rate>1 AND
  ais_memorial.noref<>'' AND ais_memorial.rate<>if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate)
  AND ifnull(mx.nobkt,'')=''  AND NOT (SUBSTRING(ais_memorial.nobkt,1,1)='T' AND SUBSTRING(ais_memorial.noref,1,1)='T') AND NOT (ais_memorial.skada LIKE '0002%' OR ais_memorial.skada LIKE '0001%' OR ais_memorial.skada LIKE '6%' OR ais_memorial.skada LIKE '7%' OR ais_memorial.skada LIKE '8%' OR ais_memorial.skada LIKE '9%')
";

$SQL .= " GROUP BY ais_memorial.noref, ais_memorial.nobkt";
$SQLCount .= " GROUP BY ais_memorial.nobkt, ais_memorial.noref";

$SQL .= " ORDER BY ais_memorial.tglbkt DESC ";
//Select Customer
//$SQLDept = "SELECT * FROM tbldept";
//print $SQL;
//******* SHOW FORM CONTENT *********
$PageNum = 1;
$result = mysql_query($SQL);
$RecCnt = mysql_num_rows($result);

//$RecCnt = $DBConnection->dbc->get_var($SQLCount);
if (!$RecCnt) $RecCnt=0;
if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);

$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
//$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize);
$TBS->Show();


function ExchangeRecord ($sequence_name,$PageSize) {
    global $DBConnection,$FileName,$TableName;
        if (GetParam("PageNum","")) {
          $StartRecord = GetParam("PageNum","")*$PageSize-$PageSize+1;
          $EndRecord   = $StartRecord+$PageSize-1;
        }
    for ($x=$StartRecord;$x<=$EndRecord;$x++) {
        if (GetParam($sequence_name.$x,"")) {
                  if (!CheckMX(GetParam($sequence_name.$x,"")))
            ReadRecord(GetParam($sequence_name.$x,""));
        }
    }
    header("Location: $FileName");
    exit;
}

function GetBaseCurrency() {
    global $DBConnection;
    $SQL = "SELECT curr FROM ais_kurssi WHERE tglbkt <= '".date("Y-m-d")."' AND rate=1 ORDER BY tglbkt DESC LIMIT 1";
    return $DBConnection->dbc->get_var($SQL);
}

function ReadRecord ($nobkt) {
    global $DBConnection;
/*
    $SQL = "
        SELECT bkt.rate as act_rate, if (SUBSTRING(bkt.nobkt,1,1)='B' && SUBSTRING(bkt.noref,1,1)='B',refmm.rate,ref.rate) as pay_rate, bkt.jumlah,bkt.noref, bkt.nobkt,
               if ((bkt.rate-if (SUBSTRING(bkt.nobkt,1,1)='B' && SUBSTRING(bkt.noref,1,1)='B',refmm.rate,ref.rate))<0,(bkt.rate-if (SUBSTRING(bkt.nobkt,1,1)='B' && SUBSTRING(bkt.noref,1,1)='B',refmm.rate,ref.rate))*-1,(bkt.rate-if (SUBSTRING(bkt.nobkt,1,1)='B' && SUBSTRING(bkt.noref,1,1)='B',refmm.rate,ref.rate))) as selisih_rate,bkt.tglbkt as tglbkt
        FROM ais_memorial bkt, ais_memorial ref
        LEFT JOIN ais_memorial refmm ON (refmm.noref=bkt.noref AND refmm.urut=1 AND refmm.nobkt like 'MM%')
        WHERE bkt.noref=ref.nobkt AND bkt.nobkt='$nobkt' AND bkt.skada=ref.skada
        GROUP BY bkt.noref
        ";
*/

$SQL = "
  SELECT ais_memorial.nobkt, ais_memorial.noref, if (sum(ais_memorial.jumlah)>sum(ref.jumlah),sum(ref.jumlah),sum(ais_memorial.jumlah)) as jumlah,
             ais_memorial.rate as forex, ais_memorial.tglbkt as date,
             if (sum(ais_memorial.jumlah)>sum(ref.jumlah),sum(ref.jumlah),sum(ais_memorial.jumlah))*(if (SUBSTRING(ais_memorial.nobkt,1,1)='T',ais_memorial.rate-if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate),if (SUBSTRING(ais_memorial.nobkt,1,1)='N',ais_memorial.rate-if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate),if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate)-ais_memorial.rate))) as amount,
             ais_memorial.debnam,
             ais_memorial.rate as rate
  FROM ais_memorial
  LEFT JOIN ais_memorial ref ON (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada AND ais_memorial.uraian=ref.uraian) OR (ref.nobkt=ais_memorial.noref AND ais_memorial.skada=ref.skada)
  LEFT JOIN ais_memorial refmm ON (refmm.noref=ais_memorial.noref AND refmm.urut=999 AND refmm.nobkt like 'MM%')
  LEFT JOIN ais_memorial mx ON mx.noref=ais_memorial.nobkt AND mx.nobkt like 'MX%'
  WHERE ".QueryLedger("ais_memorial")." AND
  ais_memorial.rate>1 AND
  ais_memorial.noref<>'' AND ais_memorial.rate<>if (SUBSTRING(ais_memorial.nobkt,1,1)='B' && SUBSTRING(ais_memorial.noref,1,1)='B',refmm.rate,ref.rate)
  AND ifnull(mx.nobkt,'')='' AND NOT (SUBSTRING(ais_memorial.nobkt,1,1)='T' AND SUBSTRING(ais_memorial.noref,1,1)='T') AND NOT (ais_memorial.skada LIKE '0002%' OR ais_memorial.skada LIKE '0001%' OR ais_memorial.skada LIKE '6%' OR ais_memorial.skada LIKE '7%' OR ais_memorial.skada LIKE '8%' OR ais_memorial.skada LIKE '9%')
  AND ais_memorial.nobkt='$nobkt'
  GROUP BY ais_memorial.noref, ais_memorial.nobkt
";

//    print $SQL;
            $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
            if ($MyResult){
                foreach ($MyResult as $RowResult) {

                     UpdateRecord ($RowResult["nobkt"],$RowResult["noref"]);
                }
            }

}

function CheckMX($nobkt) {
      global $DBConnection;
    $SQL = "SELECT COUNT(*) FROM ais_memorial WHERE noref='$nobkt' AND nobkt like 'MX%'";
    return $DBConnection->dbc->get_var($SQL);

}

function UpdateRecord ($nobkt,$noref) {
    global $DBConnection,$FileName,$TableName;

    $SQL = "
        SELECT bkt.nobkt, bkt.rate as act_rate, if (SUBSTRING(bkt.nobkt,1,1)='B' && SUBSTRING(bkt.noref,1,1)='B',refmm.rate,ref.rate) as pay_rate, if (sum(bkt.jumlah)>sum(ref.jumlah),sum(ref.jumlah),sum(bkt.jumlah)) as jumlah,
               if ((bkt.rate-if (SUBSTRING(bkt.nobkt,1,1)='B' && SUBSTRING(bkt.noref,1,1)='B',refmm.rate,ref.rate))<0,(bkt.rate-if (SUBSTRING(bkt.nobkt,1,1)='B' && SUBSTRING(bkt.noref,1,1)='B',refmm.rate,ref.rate))*-1,(bkt.rate-if (SUBSTRING(bkt.nobkt,1,1)='B' && SUBSTRING(bkt.noref,1,1)='B',refmm.rate,ref.rate))) as selisih_rate,
               bkt.tglbkt as tglbkt
        FROM ais_memorial bkt, ais_memorial ref
        LEFT JOIN ais_memorial refmm ON (refmm.noref=bkt.noref AND refmm.urut=1 AND refmm.nobkt like 'MM%')
        WHERE bkt.noref=ref.nobkt AND bkt.nobkt='$nobkt' AND bkt.skada=ref.skada AND bkt.noref='$noref'
        AND bkt.rate<>ref.rate
        AND NOT (bkt.skada LIKE '0002%' OR bkt.skada LIKE '0001%' OR bkt.skada LIKE '6%' OR bkt.skada LIKE '7%' OR bkt.skada LIKE '8%' OR bkt.skada LIKE '9%')
        GROUP BY bkt.noref
        ";

    //print $SQL;
    $rate = $DBConnection->dbc->get_row($SQL);

    $source = "ais_memorial";
    $target = "ais_memorial_temp";
    $SQL = "DROP TABLE IF EXISTS `$target`;";
    $DBConnection->dbc->query($SQL);

    $SQL = SQLDuplicateStructure($source, $target, "0", "selisih INT(11) default NULL, is999 INT(11) default NULL","memorial_id");
    $DBConnection->dbc->query($SQL);
/*
    if ($DBConnection->dbc->get_var("SELECT COUNT(*) FROM ais_memorial WHERE nobkt='$nobkt' AND noref='$noref' AND urut<>999")) {
        $SQL = "INSERT INTO $target SELECT ".GetColumnStruct($source,"memorial_id").",rate-".$rate->act_rate.",'0' FROM ais_memorial WHERE nobkt='$nobkt' AND noref='$noref' AND (skada like '0%' OR skada like '1%' OR skada like '2%' OR skada like '3%' OR skada like '4%' OR skada like '5%') AND NOT (skada like '0001%' OR skada like '0002%')";
    } else {
        $SQL = "INSERT INTO $target (".GetColumnStruct($source,"memorial_id,urut").",selisih,is999,urut) SELECT ".GetColumnStruct($source,"memorial_id,urut").",rate-".$rate->act_rate.",'0','1' FROM ais_memorial WHERE nobkt='$nobkt' AND noref='$noref' AND urut=999 ";
    }
*/

    $SQL = "INSERT INTO $target SELECT ".GetColumnStruct($source,"memorial_id").",rate-".$rate->act_rate.",'0' FROM ais_memorial WHERE nobkt='$nobkt' AND noref='$noref' AND (skada like '0%' OR skada like '1%' OR skada like '2%' OR skada like '3%' OR skada like '4%' OR skada like '5%') AND NOT (skada like '0001%' OR skada like '0002%')";
    $DBConnection->dbc->query($SQL);

/*
    ereg("TB|TK|NK|BB|BK|MC|MM|MR",$nobkt,$Mod);
    ereg("TB|TK|NK|BB|BK|MC|MM|MR",$noref,$ModRef);
*/

    list($Year,$Month,$Day) = explode("-",$rate->tglbkt);
    $TransDate = "$Day/$Month/$Year";

    $ActCode    = $DBConnection->dbc->get_var("SELECT nomemx FROM tblprofile WHERE ktrasl='".GetOfficeID()."'");
    $TransNo = SaveNumber("MX", $ActCode, $TransDate);
    $BaseCurrency = GetBaseCurrency();
    if ($rate->act_rate>$rate->pay_rate) {

        $SQL = "UPDATE $target SET urut=1,noref=nobkt,rate=1,dk='D',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' AND (skada like '0%' OR skada like '1%' OR skada like '2%') ";
        $DBConnection->dbc->query($SQL);
        $SQL = "UPDATE $target SET urut=1,noref=nobkt,rate=1,dk='K',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' AND (skada like '3%' OR skada like '4%' OR skada like '5%') ";
        $DBConnection->dbc->query($SQL);
    } else {
        if (ereg("BB|BK",$nobkt)) {
            $SQL = "UPDATE $target SET urut=1,noref=nobkt,rate=1,dk='D',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' AND (skada like '0%' OR skada like '1%' OR skada like '2%') ";
        }else
            $SQL = "UPDATE $target SET urut=1,noref=nobkt,rate=1,dk='K',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' AND (skada like '0%' OR skada like '1%' OR skada like '2%') ";
        $DBConnection->dbc->query($SQL);
        $SQL = "UPDATE $target SET urut=1,noref=nobkt,rate=1,dk='D',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' AND (skada like '3%' OR skada like '4%' OR skada like '5%') ";
        $DBConnection->dbc->query($SQL);
    }

/*
    if (($Mod[0]=="TB") || ($Mod[0]=="TK")) {
            if ($rate->act_rate>$rate->pay_rate)
            $SQL = "UPDATE $target SET noref=nobkt,rate=1,dk='D',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' ";
            else
            $SQL = "UPDATE $target SET noref=nobkt,rate=1,dk='K',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' ";
    } elseif (($Mod[0]=="BB") || ($Mod[0]=="BK") || ($Mod[0]=="MC") || ($Mod[0]=="MM") || ($Mod[0]=="MR")) {
        if (($ModRef[0]=="BK") || ($ModRef[0]=="BB")) {
            if ($rate->act_rate>$rate->pay_rate)
            $SQL = "UPDATE $target SET noref=nobkt,rate=1,dk='D',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' ";
            else
            $SQL = "UPDATE $target SET noref=nobkt,rate=1,dk='K',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' ";
        } else {
            if ($rate->act_rate>$rate->pay_rate)
            $SQL = "UPDATE $target SET noref=nobkt,rate=1,dk='K',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' ";
            else
            $SQL = "UPDATE $target SET noref=nobkt,rate=1,dk='D',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' ";
        }
    } elseif ($Mod[0]=="NK") {
            if ($rate->act_rate>$rate->pay_rate)
            $SQL = "UPDATE $target SET noref=nobkt,rate=1,dk='D',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' ";
            else
            $SQL = "UPDATE $target SET noref=nobkt,rate=1,dk='K',rupiah=".$rate->selisih_rate*$rate->jumlah.",jumlah=".$rate->selisih_rate*$rate->jumlah.",nobkt='$TransNo',curr='$BaseCurrency' WHERE nobkt='$nobkt' ";
        }

        $DBConnection->dbc->query($SQL);
*/


        $SQL = "INSERT INTO $target (".GetColumnStruct($source,"memorial_id,urut").",selisih,is999,urut) SELECT ".GetColumnStruct($source,"memorial_id,urut").",rate-".$rate->act_rate.",'1','999' FROM ais_memorial WHERE nobkt='$nobkt' LIMIT 1";
        $DBConnection->dbc->query($SQL);


        $SQL = "UPDATE $target SET nobkt='$TransNo' WHERE nobkt='$nobkt' AND is999='1' ";
        $DBConnection->dbc->query($SQL);

        $SQL = "UPDATE $target SET uraian='SELISIH KURS' WHERE nobkt like 'MX%' ";
        $DBConnection->dbc->query($SQL);

        //Selisih kurs Advanced
        if (ereg("MM",$nobkt) && ereg("BB|BK",$noref)) {
            $SQL = "UPDATE $target SET skada='".GetBSParam()."' WHERE nobkt='$TransNo' AND is999='1'";
            $DBConnection->dbc->query($SQL);
        }


        $SQL = "SELECT SUM(if(dk='D',jumlah,0)) - SUM(if(dk='K',jumlah,0)) as total FROM $target WHERE nobkt='$TransNo' AND is999<>1 ";
        $Total999 = $DBConnection->dbc->get_var($SQL);
        //$DBConnection->dbc->debug();
        if ($Total999<0)
        $SQL = "UPDATE $target SET jumlah=$Total999*-1, curr='$BaseCurrency', noref='$nobkt',rate=1, rupiah=$Total999*-1,dk='D',skada='90040000' WHERE nobkt='$TransNo' AND is999=1";
        elseif ($Total999==0)
        $SQL = "UPDATE $target SET jumlah=0, curr='$BaseCurrency', noref='$nobkt',rate=1, rupiah=0, dk='K',skada='90040000' WHERE nobkt='$TransNo'  AND is999=1";
        else
        $SQL = "UPDATE $target SET jumlah=$Total999, curr='$BaseCurrency', noref='$nobkt',rate=1, rupiah=$Total999, dk='K',skada='90040000' WHERE nobkt='$TransNo'  AND is999=1";


    $DBConnection->dbc->query($SQL);

    $SQL = "ALTER TABLE $target DROP selisih";
    $DBConnection->dbc->query($SQL);

    $SQL = "ALTER TABLE $target DROP is999";
    $DBConnection->dbc->query($SQL);

    $Fields = GetColumnStruct($target,"memorial_id");
    $SQL = "INSERT INTO $source ($Fields) SELECT $Fields FROM $target ";
    $DBConnection->dbc->query($SQL);

}


function m_event_blg ($BlockName,&$CurrRec,&$DetailSrc,$RecNum) {
    global $FileName;

        if (ereg("MM",$CurrRec["nobkt"]) && ereg("BK|BB|ND",$CurrRec["noref"])) {
            $CurrRec["amount"] = $CurrRec["amount"]*-1;
        }

        if ($CurrRec["amount"]<0)
        $CurrRec["amount"] = "(".FormatNumber($CurrRec["amount"]*-1,2).")";
}

?>
